<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Product_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $product_ref = 0;
	var $price = '';
	var $package_ids = '';
	var $description = '';
	var $created_time = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setProductRef($input){
		$this->product_ref = $input;
	}
	function setPrice($input){
		$this->price = $input;
	}
	function setPackageIds($input){
		$this->package_ids = $input;
	}
	function setDescription($input){
		$this->description = $input;
	}
	function setCreatedTime($input){
		$this->created_time = $input;
	}
	
	function getId(){
		return $this->id;
	}
	function getProductRef(){
		return $this->product_ref;
	}
	function getPrice(){
		return $this->price;
	}
	function getPackageIds(){
		return $this->package_ids;
	}
	function getDescription(){
		return $this->description;
	}
	function getCreatedTime(){
		return $this->created_time;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_PRODUCT)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->product_ref = $result[0]->product_ref;
			$this->price = $result[0]->price;
			$this->package_ids = $result[0]->package_ids;
			$this->description = $result[0]->description;
			$this->created_time = $result[0]->created_time;
			return $this;
		}
		return false;
	}
	
	function getList($limit = array(),$base_where = array()){
		$ci=& get_instance();
		if(isset($base_where['ref'])){
			$ci->db->like('product_ref', $base_where['ref']);
		}
		if(isset($base_where['from'])){
			$ci->db->where('created_time >= ', strtotime($base_where['from']));
		}
		if(isset($base_where['to'])){
			$ci->db->where('created_time <= ', strtotime($base_where['to']));
		}
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_time', 'DESC');
		return $ci->db->get(TBL_PRODUCT)->result();
	}
	function count($base_where){
		$ci=& get_instance();
		if(isset($base_where['ref'])){
			$ci->db->like('product_ref', $base_where['ref']);
		}
		if(isset($base_where['from'])){
			$ci->db->where('created_time > ', strtotime($base_where['from']));
		}
		if(isset($base_where['to'])){
			$ci->db->where('created_time < ', strtotime($base_where['to']));
		}
		return $ci->db->count_all_results(TBL_PRODUCT);
	}
	
	function save(){
		$ci=& get_instance();
		$this->product_ref = $result[0]->product_ref;
		$this->price = $result[0]->price;
		$this->package_ids = $result[0]->package_ids;
		$this->description = $result[0]->description;
		$this->created_time = $result[0]->created_time;
		$ci->db->insert(TBL_PRODUCT);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$this->price = $result[0]->price;
		$this->package_ids = $result[0]->package_ids;
		$this->description = $result[0]->description;
		return $ci->db->where('id', $this->id)->update(TBL_PRODUCT);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_PRODUCT);
	}
}